package com.example.taluo.common;

import com.example.taluo.entity.dto.gamedto.PlayerState;
import com.example.taluo.logic.GameLogic;

import java.util.LinkedList;
import java.util.Queue;

/*** @Author supersaiya
 * @Description 匹配

 * @Date 2025/2/18 17:09

 * @Version 1.0*/
public class MatchmakingManager {
    private Queue<PlayerState> waitingQueue = new LinkedList<>();
    private GameLogic gameLogic;

    public MatchmakingManager(GameLogic gameLogic) {
        this.gameLogic = gameLogic;
    }

    public synchronized void addPlayerToQueue(PlayerState player) {
        waitingQueue.add(player);
        if (waitingQueue.size() >= 2) {
            startGame(waitingQueue.poll(), waitingQueue.poll());
        }
    }

    private void startGame(PlayerState playerA, PlayerState playerB) {
        // 初始化游戏逻辑
        //gameLogic.initializeGame();

        // 发送初始状态到前端
        sendInitialStateToPlayers(playerA, playerB);
    }

    private void sendInitialStateToPlayers(PlayerState playerA, PlayerState playerB) {
        // 示例：通过WebSocket发送初始状态
        //playerA.getChannel().writeAndFlush("Initial state: " + playerA.getHand());
        //playerB.getChannel().writeAndFlush("Initial state: " + playerB.getHand());
    }
}